home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK2.toast / Development Kits (Disc 2) / QuickDraw GX / Programming Stuff / Sample Code / Graphics Samples / Bitmap Shape with Clip ƒ / Bitmap Shape with Clip.c next >
Encoding:
C/C++ Source or Header  |  1996-04-15  |  7.7 KB  |  228 lines  |  [TEXT/KAHL]

  1. /**
  2.  --
  3.  --        App:        Bitmap Shape with Clip
  4.  --
  5.  -- 
  6.  --        File:        Bitmap Shape with Clip.c
  7.  --
  8.  --
  9.  --        Comments:    This code the ability of QuickDraw GX to clip any shape with any geometric shape. 
  10.  --                    In this case, we retrieve a bitmap shape from the resource fork of the application, 
  11.  --                    and clip it with a text shape - "BEACH". We collect both shapes into a GX picture, 
  12.  --                    thereby allowing us to make one call to draw both shapes.retrieves a bitmap shape
  13.  --                    from the resource fork of the app. It creates the text shape 
  14.  --
  15.  --
  16.  --                            4/96 bob    Updated #includes to support changed GX Library names.
  17.  --                                        Changed fixed to Fixed.
  18.  --                                        Updated the note regarding the files needed to run, and copyright date.
  19.  --
  20.  --        Version:    1.0        4/94:    added the capability to dynamically determine the amount of
  21.  --                                    scaling required to make the clip shape clip the entire bitmap
  22.  --                                    shape.    
  23.  --
  24.  --                            3/93:    created     
  25.  --
  26.  --
  27.  --        Components:    Bitmap Shape with Clip.c
  28.  --                    graphics shell.c
  29.  --                    graphics shell.h
  30.  --
  31.  --
  32.  --        QuickDraw GX
  33.  --        Libraries
  34.  --        Used:        This application uses the following QuickDraw GX library code files:
  35.  --                    "ColorLibrary.c", "FontLibrary.c", "GraphicsDebugLibrary.c",
  36.  --                    "QDLibrary.c", and "TransformLibrary.c".
  37.  --        
  38.  --        
  39.  --        Notes:        1) Print this file in landscape for the best results
  40.  --                    2) If you are using THINK C v5.x, I have added THINK markers to navigate the code.
  41.  --                    3) This code was adapted from the "One Rectangle" QuickDraw GX sample.
  42.  --
  43.  --
  44.  --        Author:        Pete "Luke" Alexander
  45.  --                    Developer Technical Support
  46.  --                    AppleLink: DEVSUPPORT
  47.  --
  48.  --        
  49.  --        ©1992 - 1996  Apple Computer, Inc. 
  50.  --        All rights reserved.
  51.  --
  52.  **/
  53.  
  54. #include <events.h>
  55. #include <windows.h>
  56.  
  57. #include "FontLibrary.h" 
  58. #include <GXErrors.h>
  59. #include "GraphicsLibraries.h"
  60. #include <GXEnvironment.h>
  61. #include "QDLibrary.h"
  62. #include "graphics shell.h"
  63.  
  64. #define kCheckBitmapShape
  65.  
  66. //
  67. //  Set up the title and size of the window 
  68. //
  69. Str255         gWindowTitle = "\p Bitmap Shape with Clip ";
  70. Rect         gWindowQDRect  = {50, 20, 345, 455};
  71.  
  72. //
  73. //    gGraphicsHeapSize sets the size of the graphics gxHeap created by calling the GXNewGraphicsClient routine
  74. //    in main () within graphics shell.c.  You can determine the amount of graphics gxHeap required by using GraphicsBug.
  75. //    With  gGraphicsHeapSize set to 48k,I had 6 free blocks left in the graphics gxHeap and
  76. //    I was not receiving any memory related warnings or notices from GX....
  77. //
  78. long        gGraphicsHeapSize = 48;
  79.  
  80. gxShape     gthePicture;
  81.  
  82.  
  83.  
  84. /*------ DoInitialization ---------------------------------------------------------------------------------*/
  85.  
  86. void DoInitialization(gWindow)
  87. WindowPtr gWindow;
  88. {
  89.     gxShape        newClipShape, clipShapeOutline;
  90.     gxShape        tempBitmapShape;
  91.     float        xScaleFactor, yScaleFactor;
  92.     Fixed         clipShapeWidth,clipShapeHeight;
  93.     Fixed         bitmapWidth, bitmapHeight;
  94.     gxRectangle    bitmapBounds, clipShapeBounds;
  95.  
  96.  
  97.     InitCommonColors ();
  98.     
  99.     gthePicture = GXNewShape(gxPictureType);
  100.  
  101.     //
  102.     //    Retrieve the bitmap shape form the resource fork of the application. With the "debugging" init installed, we
  103.     //    can check to see if the shape was retrieved correctly by calling the shape validation routine. If the shape is
  104.     //    not valid, we will recieve a warning in the debugger.
  105.     //
  106.     tempBitmapShape = GetPixMapShape(129);
  107.     
  108.     #ifdef kCheckBitmapShape
  109.         GXValidateShape (tempBitmapShape);
  110.     #endif
  111.  
  112.     //
  113.     //    Define the text shape to clip our bitmap shape with. In this case, our clip shape will be a text
  114.     //    shape which uses a text size of 96 point and the Helvetica font.
  115.     //
  116.     newClipShape = GXNewText( 5,(unsigned char*)"BEACH", nil );
  117.     SetShapeCommonFont( newClipShape, helveticaFont );
  118.     GXSetShapeTextSize( newClipShape, ff(96) );
  119.  
  120.     //
  121.     //    Determine the bounds of our bitmap and clip shape and move the clip shape to the top left corner 
  122.     //    of the bitmap shape. 
  123.     //
  124.     GXGetShapeBounds( tempBitmapShape, 0, &bitmapBounds );
  125.     GXGetShapeBounds( newClipShape, 0, &clipShapeBounds );
  126.  
  127.     clipShapeHeight = clipShapeBounds.bottom - clipShapeBounds.top;
  128.     
  129.     GXMoveShapeTo ( newClipShape, bitmapBounds.left - ff(7), clipShapeHeight + bitmapBounds.top  - ff(2) );
  130.  
  131.     //
  132.     //    We turn off the metrics and contour capabilites of TrueType to enable GX to
  133.     //    scale the clip shape linearly. We convert our text shape to a path shape because
  134.     //    you cannot use a text shape as a clip shape. A clip shape can only be a geometric shape
  135.     //    (i.e. a point, line, rectangle, polygon, curve, or path).
  136.     //
  137.     GXSetShapeTextAttributes ( newClipShape, gxNoMetricsGridText | gxNoContourGridText );
  138.     GXPrimitiveShape ( newClipShape );
  139.     
  140.     //
  141.     //    Since, we just moved the clip shape, we need to determine the bounds of our shape again.
  142.     //    We can now determine the amount we need to scale our clip shape to cover our bitmap
  143.     //    shape. This is accomplished by determining the differences between the height and width
  144.     //    of the bitmap and clip shapes. We will then use these differences as the scale factors
  145.     //    when we call GXScaleShape (..) to scale up our clip shape.
  146.     //
  147.     //    The "fl" macro converts a floating point number into a fixed point number.
  148.     //
  149.     GXGetShapeBounds( newClipShape, 0, &clipShapeBounds );
  150.  
  151.     clipShapeWidth = clipShapeBounds.right - clipShapeBounds.left;
  152.     bitmapWidth = bitmapBounds.right - bitmapBounds.left;
  153.     xScaleFactor = FixedToFloat( bitmapWidth ) / FixedToFloat ( clipShapeWidth );
  154.     
  155.     clipShapeHeight = clipShapeBounds.bottom - clipShapeBounds.top;
  156.     bitmapHeight = bitmapBounds.bottom - bitmapBounds.top;
  157.     yScaleFactor = FixedToFloat( bitmapHeight ) / FixedToFloat ( clipShapeHeight );
  158.  
  159.     GXScaleShape( newClipShape, fl(xScaleFactor),  fl(yScaleFactor), bitmapBounds.left, bitmapBounds.top );
  160.  
  161.     //
  162.     // Set the clip of our bitmap shape to our text shape and add it to our picture.
  163.     //
  164.     GXSetShapeClip( tempBitmapShape, newClipShape );
  165.     GXSetPictureParts(gthePicture, 0, 0, 1, &tempBitmapShape, nil, nil, nil );
  166.     GXDisposeShape ( tempBitmapShape );
  167.  
  168.     //
  169.     //    Change the fill of our text shape be to the outline of the text, set the size used, set the pen to cruise on the outside
  170.     //    of the contour of each letter, and add it to our picture shape.
  171.     //
  172.     GXSetShapeFill( newClipShape, gxClosedFrameFill );
  173.     GXSetShapeStyleAttributes( newClipShape, gxOutsideFrameStyle );
  174.     GXSetShapePen( newClipShape, ff(3));
  175.     SetShapeCommonColor( newClipShape, blue);
  176.  
  177.     GXSetPictureParts(gthePicture, 0, 0, 1, &newClipShape, nil, nil, nil);
  178.     GXDisposeShape ( newClipShape );
  179.  
  180.     GXMoveTransform(GXGetShapeTransform( gthePicture ), ff(20), ff(15) );    
  181. }
  182.  
  183.  
  184. /*------ DoDraw ---------------------------------------------------------------------------------------*/
  185.  
  186. void DoDraw(gWindow)
  187. WindowPtr gWindow;
  188. {
  189.     GXDrawShape (gthePicture);
  190. }
  191.  
  192.  
  193. /*------ DoDispose -------------------------------------------------------------------------------------*/
  194.  
  195. void DoDispose(gWindow)
  196. WindowPtr gWindow;
  197. {
  198.     /**  
  199.         You should always dispose of your GX graphics objects before tossing your window. Why? It's generally good 
  200.         form and this approach guarantees that everything is disposed. If you had not disposed of everything, the
  201.         call to DisposeWindow should dispose of the objects. If you are running the debugging version of the 
  202.         SecretGraphics init with notices set, you will receive a notice that you had not disposed of everything. You
  203.         can turn notices on in this file by setting gDebugging = TRUE (above).
  204.     **/
  205.     DisposeCommonColors ();
  206.     GXDisposeShape(gthePicture);  
  207.      GXDisposeShape(gWindowBoundsShape);  
  208.     DisposeWindow(gWindow);
  209. }
  210.     
  211.  
  212.  
  213. /*------ DoClick ---------------------------------------------------------------------------------------*/
  214.  
  215. void DoClick( orgMouseLoc, theWindow )
  216. gxPoint        orgMouseLoc;
  217. WindowPtr     theWindow;
  218. {
  219. }
  220.  
  221.  
  222. /*------ DoIdle ----------------------------------------------------------------------------------------*/
  223.  
  224. void DoIdle(gWindow)
  225. WindowPtr gWindow;
  226. {
  227. }
  228.